local skel = fk.CreateSkill {
  name = "emo__jingce",
  dynamic_desc = function (self, player, lang)
    local mark = player:getMark("@emo__jingce")
    if mark == 0 then mark = "emo__jingce_other" end
    return mark
  end,
}

Fk:loadTranslationTable{
  ["emo__jingce"] = "精策",
  [":emo__jingce"] = "根据你上次失去牌的方式，你获得以下效果："..
  "使用→你受到伤害时移动场上一张牌；打出→你响应牌时获得之；弃置→你失去牌时摸两张牌；不为以上项，你获得牌时回复一点体力。",

  [":emo__jingce_use"] = "根据你上次失去牌的方式，你获得以下效果："..
  "<font color='#E0DB2F'>使用→你受到伤害时移动场上一张牌；</font><font color='grey'>打出→你响应牌时获得之；弃置→你失去牌时摸两张牌；不为以上项，你获得牌时回复一点体力。</font>",
  [":emo__jingce_resp"] = "根据你上次失去牌的方式，你获得以下效果："..
  "<font color='grey'>使用→你受到伤害时移动场上一张牌；</font><font color='#E0DB2F'>打出→你响应牌时获得之；</font><font color='grey'>弃置→你失去牌时摸两张牌；不为以上项，你获得牌时回复一点体力。</font>",
  [":emo__jingce_throw"] = "根据你上次失去牌的方式，你获得以下效果："..
  "<font color='grey'>使用→你受到伤害时移动场上一张牌；打出→你响应牌时获得之；</font><font color='#E0DB2F'>弃置→你失去牌时摸两张牌；</font><font color='grey'>不为以上项，你获得牌时回复一点体力。</font>",
  [":emo__jingce_other"] = "根据你上次失去牌的方式，你获得以下效果："..
  "<font color='grey'>使用→你受到伤害时移动场上一张牌；打出→你响应牌时获得之；弃置→你失去牌时摸两张牌；</font><font color='#E0DB2F'>不为以上项，你获得牌时回复一点体力。</font>",

  ["@emo__jingce"] = "精策",
  ["emo__jingce_use"] = "使用",
  ["emo__jingce_resp"] = "打出",
  ["emo__jingce_throw"] = "弃置",
  ["#emo__jingce-move"] = "精策：你可以移动场上一张牌",

  ["$emo__jingce1"] = "精细入微，策敌制胜。",
  ["$emo__jingce2"] = "妙策如神，精兵强将，安有不胜之理？",
}

-- 受到伤害时，移动场上一张牌
skel:addEffect(fk.DamageInflicted, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:getMark("@emo__jingce") == "emo__jingce_use"
  end,
  on_cost = function (self, event, target, player, data)
    local tos = player.room:askToChooseToMoveCardInBoard(player, {
      skill_name = skel.name, prompt = "#emo__jingce-move",
    })
    if #tos == 2 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local tos = event:getCostData(self).tos
    if not tos or #tos ~= 2 then return end
    player.room:askToMoveCardInBoard(player, {
      target_one = tos[1], skill_name = skel.name, target_two = tos[2],
    })
  end,
})

-- 使用响应牌时，获得之
skel:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and
    data.extra_data and data.extra_data.emo__jingce_obtain
    and data.responseToEvent and data.responseToEvent.card
    and player.room:getCardArea(data.responseToEvent.card) == Card.Processing
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:obtainCard(player, data.responseToEvent.card, true, fk.ReasonPrey, player, skel.name)
  end,
})

--- 使用牌之前，就得判断好。不然mark会变成“使用”，而无法获得“打出”mark的效果
skel:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:getMark("@emo__jingce") == "emo__jingce_resp"
    and data.responseToEvent and data.responseToEvent.card
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.emo__jingce_obtain = true
  end,
})

-- 打出响应牌时，获得之
skel:addEffect(fk.CardResponding, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:getMark("@emo__jingce") == "emo__jingce_resp"
    and data.responseToEvent and data.responseToEvent.card and player.room:getCardArea(data.responseToEvent.card) == Card.Processing
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:obtainCard(player, data.responseToEvent.card, true, fk.ReasonPrey, player, skel.name)
  end,
})

--- 若上次是其他失去牌，则本次获得牌则回1血
--- 若上次是弃置失去牌，则本次失去牌则摸2张牌
skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    local lose, get = false, false
    local reason -- 上次失去牌的原因
    for _, move in ipairs(data) do
      if move.from == player and table.find(move.moveInfo, function(info)
        return info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand
      end) then
        lose = true
      end
      if move.to == player and move.toArea == Card.PlayerHand and #move.moveInfo > 0 then
        get = true
      end
    end
    local current = player.room.logic:getCurrentEvent()
    -- 用记录器读取上次失去牌的原因，防止插结
    player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      if e.id >= current.id then return false end
      for _, move in ipairs(e.data) do
        if move.from == player and table.find(move.moveInfo, function(info)
          return info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand
        end) then
          reason = move.moveReason
          return true
        end
      end
    end, 1)
    if reason == fk.ReasonDiscard and lose then
      event:setCostData(self, { choice = "draw" })
      return true
    elseif get and player:isWounded() and not table.contains({fk.ReasonUse, fk.ReasonDiscard, fk.ReasonResponse}, reason) then
      event:setCostData(self, { choice = "recover" , anim_type = "defensive" })
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    if choice == "recover" then
      room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
    elseif choice == "draw" then
      player:drawCards(2, skel.name)
    end
  end,
})

--- 失去牌时，记录失去牌的原因
skel:addEffect(fk.AfterCardsMove, {
  late_refresh = true, -- 技能结算后再计入标记
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local map = {
      [fk.ReasonUse] = "emo__jingce_use",
      [fk.ReasonDiscard] = "emo__jingce_throw",
      [fk.ReasonResponse] = "emo__jingce_resp",
    }
    for _, move in ipairs(data) do
      if move.from == player and table.find(move.moveInfo, function(info)
        return info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand
      end) then
        local reason = map[move.moveReason] or 0
        room:setPlayerMark(player, "@emo__jingce", reason)
        return
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__jingce", 0)
end)

return skel
